package com.runjian.rundo.iot.agent.common.message;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson2.annotation.JSONField;

import lombok.Getter;
import lombok.Setter;

/**
 * 解码后的消息 默认实现
 *
 * @author LiGang
 */
@Getter
@Setter
public class DefaultDecodedMessage implements DecodedMessage {

    /**
     * 设备deviceId
     */
    private String deviceId;

    /**
     * 设备deviceId
     */
    private String externalId;

    /**
     * 设备网关id，默认和设备id一致
     */
    private String gatewayId;

    /**
     * 设备数据UTC时间
     */
    @JSONField(format = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
    private ZonedDateTime time;

    /**
     * 发布到 IoT 平台的测量数据
     */
    @JSONField(name = "measurements")
    private Map<String, Object> payload;

    public DefaultDecodedMessage() {
        this("", "", "");
    }

    public DefaultDecodedMessage(String deviceId, String externalId, String gatewayId) {
        this(deviceId, externalId, gatewayId, new HashMap<>());
    }

    public DefaultDecodedMessage(String deviceId, String externalId, String gatewayId, Map<String, Object> payload) {
        this(deviceId, externalId, gatewayId, LocalDateTime.now().atZone(ZoneId.systemDefault()).withZoneSameInstant(ZoneId.of("UTC")),
            payload);
    }

    public DefaultDecodedMessage(String deviceId, String externalId, String gatewayId, ZonedDateTime time, Map<String, Object> payload) {
        this.gatewayId = gatewayId;
        this.time = time;
        this.payload = payload;
        this.deviceId = deviceId;
        this.externalId = externalId;
    }

    public DefaultDecodedMessage addSeries(String series, Object value) {
        this.payload.put(series, value);
        return this;
    }

}
